package com.foreknow.service.impl;

import com.foreknow.dao.GuestBookDao;
import com.foreknow.dao.impl.GuestBookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.GuestBook;
import com.foreknow.service.GuestBookService;

import java.sql.Connection;
import java.sql.SQLException;

public class GuestBookServiceImpl implements GuestBookService {
    private GuestBookDaoImpl dao = new GuestBookDaoImpl();
    @Override
    public boolean add(GuestBook gb) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        dao.setConnection(conn);
        boolean isRight = false;
        try {
           isRight = dao.save(gb);
           //dml操作需要提交事物
           conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRight;
    }

    public static void main(String[] args) {
        GuestBookService service = new GuestBookServiceImpl();
        GuestBook gb = new GuestBook();
        gb.setName("wander");
        gb.setPhone("13322222222");
        gb.setEmail("wander@163.com");
        gb.setTitle("test");
        gb.setContent("content");
        boolean isRight = service.add(gb);
        System.out.println(isRight);
    }
}
